class Statistics
    def initialize
        @order = ["ip_src"]
        @last  = {}
    end
    def display(connections)
        x = 0.0
        y = -0.1

        @order.each do |order_field|
            index = 0
            result = aggregate(connections, order_field)
            if result != @last
                glClear GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT 
                glRasterPos2f( -0.2, -0.1 )
                (order_field + ":").each_byte{|x| GLUT.BitmapCharacter(0, x) }

                result.sort{|x, y| y[1] <=> x[1] }.each do |data|
                    glRasterPos2f( 0.0, -0.1+((-0.05)*index) )
                    data.first.each_byte{|x| GLUT.BitmapCharacter(0, x) }
                    index += 1
                end
                @last = result
             end
        end
    end
    private
    def aggregate(data, field)
        result = {}
        data.each do |tmp|
            begin
                tmp_val = tmp.send(field)
                if tmp_val
                   result.has_key?(tmp_val.to_s) ? result[tmp_val.to_s] += 1 : result[tmp_val.to_s] = 1
                end
            rescue NoMethodError => e
            end
        end
        return result
    end
end

STATISTICS = Statistics.new

